package com.wedo.mall.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.activity.EActivity;
import com.e.library.adapter.Adapter;
import com.e.library.adapter.EAdapter;
import com.e.library.listener.EListener;
import com.wedo.mall.R;
import com.wedo.mall.activity.goods.GoodsDetailActivity;
import com.wedo.mall.activity.user.MessageDetailActivity;
import com.wedo.mall.entity.SystemMessage;
import com.wedo.mall.util.DateUtils;
import com.wedo.mall.util.WUrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Lawrence on 2017/8/16.
 */

public class SystemMessageAdapter extends Adapter<SystemMessage> {

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public SystemMessageAdapter(Context context, List<SystemMessage> systemMessages) {
        super(context, systemMessages);
    }

    @Override
    protected int layout() {
        return R.layout.item_systemmessage;
    }

    @Override
    protected void bind(EHolder holder, SystemMessage message, int position) {
        TextView tv_title = holder.find(R.id.tv_title);
        TextView tv_add_time = holder.find(R.id.tv_add_time);
        TextView tv_content = holder.find(R.id.tv_content);
        ImageView iv_image = holder.find(R.id.iv_image);

        tv_add_time.setText(DateUtils.convert(format,message.add_time));
        tv_content.setText(message.content);

        if(message.system() || message.agentGoods()) {
            tv_title.setText(message.title);
            String img = WUrl.url(message.image);
            Glide.with(mContext).load(img).error(R.drawable.place_holder_banner).placeholder(R.drawable.place_holder_banner)
                    .diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_image);
        }else if(message.goods()) {
            tv_title.setText(message.goods_name);
            String img = WUrl.url(message.goods_image);
            Glide.with(mContext).load(img).error(R.drawable.place_holder_banner).placeholder(R.drawable.place_holder_banner)
                    .diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_image);
        }
    }

    public void listener(final EActivity activity){
        onItemClickListener = new EListener<SystemMessage>() {
            @Override
            public void onInvoked(View view, SystemMessage data, int position) {
                if(data.goods() || data.agentGoods()) {
                    GoodsDetailActivity.toDetail(activity,data.goods_id);
                }else {
                    MessageDetailActivity.toDetail(activity,data.id);
                }
            }
        };
    }
}
